class Solution {
public:
    ListNode* partition(ListNode* head, int x)
    {
        ListNode* less = new ListNode(0);
        ListNode* greater = new ListNode(0);
        ListNode* pLess = less;
        ListNode* pGreater = greater;
        while(head) {
            if(head->val < x) {
                pLess->next = head;
                pLess = pLess->next;
            } else {
                pGreater->next = head;
                pGreater = pGreater->next;
            }
            head = head->next;
        }

        pGreater->next = NULL;
        pLess->next = greater->next;
        return less->next;
    }
};
